package com.hustzp.com.xichuangzhu.fragment;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.feedback.FeedbackAgent;
import com.hustzp.com.xichuangzhu.R;
import com.hustzp.com.xichuangzhu.login.LoginActivity;
import com.hustzp.com.xichuangzhu.me.AboutUsActivity;
import com.hustzp.com.xichuangzhu.me.CustomQuoteListActivity;
import com.hustzp.com.xichuangzhu.me.MyStarActivity;
import com.hustzp.com.xichuangzhu.me.RecmdsListActivity;
import com.hustzp.com.xichuangzhu.me.SettingActivity;
import com.hustzp.com.xichuangzhu.me.UserInfoActivity;
import com.hustzp.com.xichuangzhu.utils.Constant;
import com.hustzp.com.xichuangzhu.utils.ImageUtils;
import com.hustzp.com.xichuangzhu.widget.XCRoundRectImageView;

/**
 * Copyright (C) 2015 The huimai mobile client Project
 * All right reserved.
 *
 * @author: wuzhimin@huimai365.com
 * @date: 2016/5/25 18:38
 * @Description:
 */
public class MyFragment extends Fragment {

    private AVUser curUser;
    private LinearLayout userLayout;
    private TextView loginLayout;

    private XCRoundRectImageView userAvatar;
    private TextView userName;
    private TextView userProfile;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_my, container, false);
        initView(view);
        initData();
        return view;
    }

    private void initView(View view) {
        userLayout = (LinearLayout) view.findViewById(R.id.user_layout);
        loginLayout = (TextView) view.findViewById(R.id.tv_login_register);

        userAvatar = (XCRoundRectImageView) view.findViewById(R.id.user_avatar);
        userName = (TextView) view.findViewById(R.id.user_name);
        userProfile = (TextView) view.findViewById(R.id.user_profile);

        // 个人信息
        RelativeLayout myProfileBtn = (RelativeLayout) view.findViewById(R.id.my_profile_btn);
        myProfileBtn.setOnClickListener(listener);

        // 我的收藏
        RelativeLayout myStarBtn = (RelativeLayout) view.findViewById(R.id.my_star_btn);
        myStarBtn.setOnClickListener(listener);

        // 创作记录
        RelativeLayout myCreateBtn = (RelativeLayout) view.findViewById(R.id.my_create_btn);
        myCreateBtn.setOnClickListener(listener);

        //推荐收录
        RelativeLayout myRecommandBtn = (RelativeLayout) view.findViewById(R.id.my_recommand_btn);
        myRecommandBtn.setOnClickListener(listener);

        // 我的反馈
        RelativeLayout myFeedbackBtn = (RelativeLayout) view.findViewById(R.id.my_feedback_btn);
        myFeedbackBtn.setOnClickListener(listener);

        // 去评价
        RelativeLayout myScoreBtn = (RelativeLayout) view.findViewById(R.id.my_score_btn);
        myScoreBtn.setOnClickListener(listener);

        // 设置
        RelativeLayout mySettingBtn = (RelativeLayout) view.findViewById(R.id.my_setting_btn);
        mySettingBtn.setOnClickListener(listener);

        // 关于
        RelativeLayout myAboutBtn = (RelativeLayout) view.findViewById(R.id.my_about_btn);
        myAboutBtn.setOnClickListener(listener);
    }

    private void initData() {
        if (curUser != null) {
            if (curUser.getAVFile("avatar") == null) {
                ImageUtils.loadImage(Constant.DEFAULT_AVATAR_URL, userAvatar);
            } else {
                ImageUtils.loadImage(curUser.getAVFile("avatar").getUrl(), userAvatar);
            }
            userName.setText(curUser.getUsername());
            userProfile.setText(curUser.getString("desc"));

            userLayout.setVisibility(View.VISIBLE);
            loginLayout.setVisibility(View.GONE);
        } else {
            ImageUtils.loadImage(Constant.DEFAULT_AVATAR_URL, userAvatar);
            loginLayout.setVisibility(View.VISIBLE);
            userLayout.setVisibility(View.GONE);
        }

    }

    @Override
    public void onResume() {
        super.onResume();
        curUser = AVUser.getCurrentUser();
        initData();
    }

    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            if (view.getId() == R.id.my_feedback_btn) {
                // 我的反馈
                FeedbackAgent agent = new FeedbackAgent(getContext());
                agent.startDefaultThreadActivity();
                return;
            } else if (view.getId() == R.id.my_score_btn) {
                // 去评价
                Intent intent = new Intent(Intent.ACTION_VIEW);
//                intent.setData(Uri.parse("market://details?id=" + getActivity().getPackageName()));
                intent.setData(Uri.parse("market://details?id=" + "com.tencent.mm")); // 暂时打开微信应用
                startActivity(intent);
                return;
            }

            boolean needLogin = false;
            Class clazz = null;
            switch (view.getId()) {
                case R.id.my_profile_btn:
                    // 我的个人信息
                    needLogin = true;
                    clazz = UserInfoActivity.class;
                    break;
                case R.id.my_star_btn:
                    // 我的收藏
                    needLogin = true;
                    clazz = MyStarActivity.class;
                    break;
                case R.id.my_create_btn:
                    // 创作记录
                    needLogin = true;
                    clazz = CustomQuoteListActivity.class;
                    break;
                case R.id.my_recommand_btn:
                    // 推荐收录
                    needLogin = true;
                    clazz = RecmdsListActivity.class;
                    break;
                case R.id.my_setting_btn:
                    // 设置
                    clazz = SettingActivity.class;
                    break;
                case R.id.my_about_btn:
                    // 关于
                    clazz = AboutUsActivity.class;
                    break;
            }

            if (needLogin && AVUser.getCurrentUser() == null) {
                startActivity(new Intent(getActivity(), LoginActivity.class));
                return;
            }

            startActivity(new Intent(getActivity(), clazz));
        }

    };

}
